﻿using QFramework;
using System;
using System.Collections.Generic;
using UnityEngine;


namespace ProjectIndieFarm
{
    public class SoilSystem : AbstractSystem, ISoilSystem
    {
        public EasyGrid<SoilData> SoilGrid { get; private set; } = new EasyGrid<SoilData>(Config.INIT_SOIL_GRID_WIDTH, Config.INIT_SOIL_GRID_HEIGHT);

        protected override void OnInit()
        {

        }

        #region IArchive
        /// <summary>
        /// 重置地块数据
        /// </summary>
        public void ResetData()
        {
            for (int i = 0; i < SoilGrid.Width; i++)
            {
                for (int j = 0; j < SoilGrid.Height; j++)
                {
                    //全部置为1 表示地块为空 不可耕种
                    PlayerPrefs.SetInt($"soil_{i}_{j}_is_empty", true ? 1 : 0);
                    PlayerPrefs.SetInt($"soil_{i}_{j}_has_plant", false ? 1 : 0);
                    PlayerPrefs.SetInt($"soil_{i}_{j}_watered", false ? 1 : 0);
                    PlayerPrefs.SetString($"soil_{i}_{j}_plant_name", string.Empty);
                    PlayerPrefs.SetInt($"soil_{i}_{j}_plant_state", 0);
                    PlayerPrefs.SetInt($"soil_{i}_{j}_days_in_current_state", 0);
                    PlayerPrefs.SetInt($"soil_{i}_{j}_days_to_next_state", 1);
                    PlayerPrefs.SetInt($"soil_{i}_{j}_ripe_count", 0);
                    SoilGrid[i, j] = null;
                }
            }
            //重置地块大小
            //SoilGrid.Resize(Config.INIT_SOIL_GRID_WIDTH, Config.INIT_SOIL_GRID_HEIGHT, (x, y) => null);
            SoilGrid = new EasyGrid<SoilData>(Config.INIT_SOIL_GRID_WIDTH, Config.INIT_SOIL_GRID_HEIGHT);

            PlayerPrefs.SetInt($"soil_grid_width", Config.INIT_SOIL_GRID_WIDTH);
            PlayerPrefs.SetInt($"soil_grid_height", Config.INIT_SOIL_GRID_HEIGHT);
        }

        /// <summary>
        /// 加载地块数据
        /// </summary>
        public void LoadData()
        {
            //地块大小
            int width = PlayerPrefs.GetInt($"soil_grid_width", Config.INIT_SOIL_GRID_WIDTH);
            int height = PlayerPrefs.GetInt($"soil_grid_height", Config.INIT_SOIL_GRID_HEIGHT);
            SoilGrid.Resize(width, height, (x, y) => null);
            //地块内容
            for (int i = 0; i < SoilGrid.Width; i++)
            {
                for (int j = 0; j < SoilGrid.Height; j++)
                {
                    bool isEmpty = PlayerPrefs.GetInt($"soil_{i}_{j}_is_empty", 1) == 1;
                    if (isEmpty)
                    {
                        SoilGrid[i, j] = null;
                    }
                    //地块不为空
                    else
                    {
                        SoilGrid[i, j] = new SoilData();

                        bool hasPlant = PlayerPrefs.GetInt($"soil_{i}_{j}_has_plant", 0) == 1;
                        bool watered = PlayerPrefs.GetInt($"soil_{i}_{j}_watered", 0) == 1;
                        SoilGrid[i, j].HasPlant = hasPlant;
                        SoilGrid[i, j].Watered = watered;

                        if (hasPlant)
                        {
                            var plantName = PlayerPrefs.GetString($"soil_{i}_{j}_plant_name", string.Empty);
                            var plantState = (PlantStates)PlayerPrefs.GetInt($"soil_{i}_{j}_plant_state", 0); //默认值 PlantStates.Seed 的值是0
                            var plantDaysInCurrentSate = PlayerPrefs.GetInt($"soil_{i}_{j}_days_in_current_state", 0);
                            var plantDaysToNextSate = PlayerPrefs.GetInt($"soil_{i}_{j}_days_to_next_state", 0);
                            var plantRipeCount = PlayerPrefs.GetInt($"soil_{i}_{j}_ripe_count", 0);

                            SoilGrid[i, j].PlantName = plantName;
                            SoilGrid[i, j].PlantState = plantState;
                            SoilGrid[i, j].DaysInCurrentState = plantDaysInCurrentSate;
                            SoilGrid[i, j].DaysToNextState = plantDaysToNextSate;
                            SoilGrid[i, j].RipeCount = plantRipeCount;
                        }

                    }
                }
            }
        }

        /// <summary>
        /// 存储地块数据
        /// </summary>
        public void SaveData()
        {
            PlayerPrefs.SetInt($"soil_grid_width", SoilGrid.Width);
            PlayerPrefs.SetInt($"soil_grid_height", SoilGrid.Height);

            for (int i = 0; i < SoilGrid.Width; i++)
            {
                for (int j = 0; j < SoilGrid.Height; j++)
                {
                    var soilData = SoilGrid[i, j];
                    //地块为空
                    if (soilData == null)
                    {
                        //空地块
                        PlayerPrefs.SetInt($"soil_{i}_{j}_is_empty", true ? 1 : 0);
                        //没植物
                        PlayerPrefs.SetInt($"soil_{i}_{j}_has_plant", false ? 1 : 0);
                    }
                    //地块不为空
                    else
                    {
                        PlayerPrefs.SetInt($"soil_{i}_{j}_is_empty", false ? 1 : 0);
                        //是否有植物
                        PlayerPrefs.SetInt($"soil_{i}_{j}_has_plant", soilData.HasPlant ? 1 : 0);
                        PlayerPrefs.SetInt($"soil_{i}_{j}_watered", soilData.Watered ? 1 : 0);
                        //如果有植物
                        if (soilData.HasPlant)
                        {
                            PlayerPrefs.SetString($"soil_{i}_{j}_plant_name", soilData.PlantName);
                            PlayerPrefs.SetInt($"soil_{i}_{j}_plant_state", (int)soilData.PlantState);
                            PlayerPrefs.SetInt($"soil_{i}_{j}_days_in_current_state", soilData.DaysInCurrentState);
                            PlayerPrefs.SetInt($"soil_{i}_{j}_days_to_next_state", soilData.DaysToNextState);
                            PlayerPrefs.SetInt($"soil_{i}_{j}_ripe_count", soilData.RipeCount);
                        }
                    }
                }
            }
        }
        #endregion
    }
}
